﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KOPLibrary;
using Microsoft.Office.Interop.Word;
using KOPLibrary.AutoComplete;
using System.IO;
using KOPLibrary.PreferenceManagement;

namespace KOP
{
    public partial class AutoCompleteManageForm : Form
    {
        AutoComplateInfoList baselist;
        int rowcount;
        public AutoCompleteManageForm(AutoComplateInfoList template, List<ContentControl> controls)
        {
            InitializeComponent();
            this.MaximumSize = new Size(334, Screen.PrimaryScreen.Bounds.Height - 100);
            baselist = template;
            int i = 0;
            ctlLayoutPanel.SuspendLayout();
            ctlLayoutPanel.MaximumSize = new Size(334, Screen.PrimaryScreen.Bounds.Height - 185);
            string controltitle;
            foreach (ContentControl control in controls)
            {                
                if (string.IsNullOrEmpty(control.Title))
                {
                    controltitle = "NoTitle;" + i.ToString(); ;
                }
                else controltitle = control.Title + ";" + i.ToString();
                AutoCompleteInfo info = template.List.FirstOrDefault(p => p.FieldName == GetTitle(controltitle));
                Label templabel = new Label();
                templabel.SuspendLayout();
                templabel.Text = GetTitle(controltitle) + " :";
                templabel.TextAlign = ContentAlignment.MiddleRight;
                templabel.Anchor = AnchorStyles.Right;
                templabel.Tag = controltitle;
                templabel.ResumeLayout();
                ctlLayoutPanel.Controls.Add(templabel, 0, i);
                ComboBox tempcombobox = new ComboBox();
                tempcombobox.SuspendLayout();
                tempcombobox.Tag = controltitle;
                tempcombobox.Name = controltitle.Replace(";", string.Empty);
                tempcombobox.Width = 176;
                tempcombobox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                tempcombobox.AutoCompleteSource = AutoCompleteSource.ListItems;
                if (info != null && info.Values.Count >= 1)
                {
                    foreach (string value in info.Values)
                    {
                        tempcombobox.Items.Add(value);
                    }
                    tempcombobox.SelectedIndex = 0;
                }
                TableLayoutPanel temppanel = new TableLayoutPanel();
                temppanel.SuspendLayout();
                temppanel.AutoSize = true;
                temppanel.RowCount = 1;
                temppanel.ColumnCount = 2;
                temppanel.Anchor = AnchorStyles.Left;
                Button tempbutton = null;
                if (control.Type == WdContentControlType.wdContentControlPicture)
                {
                    tempcombobox.Width = 147;
                    tempbutton = new Button();
                    tempbutton.SuspendLayout();
                    tempbutton.Tag = controltitle;
                    tempbutton.Size = new Size(23, 20);
                    tempbutton.Text = "...";
                    tempbutton.Click += new EventHandler(tempbutton_Click);
                    tempbutton.ResumeLayout();
                }
                tempcombobox.ResumeLayout();
                temppanel.Controls.Add(tempcombobox, 0, 0);
                if (tempbutton != null) temppanel.Controls.Add(tempbutton, 1, 0);
                temppanel.ResumeLayout();
                ctlLayoutPanel.Controls.Add(temppanel, 1, i);
                i++;

            }
            rowcount = i;
            foreach (RowStyle style in ctlLayoutPanel.RowStyles)
            {
                style.SizeType = SizeType.AutoSize;
            }
            ctlLayoutPanel.ResumeLayout();
            this.Height = ctlLayoutPanel.Height + 100;
        }

        void tempbutton_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog fd = new OpenFileDialog())
            {
                fd.Filter = "Resim Dosyaları|*.jpg;*.png;*.gif;*.bmp;*.tif;*.jpeg";
                fd.Title = "Resim Ekle";
                fd.InitialDirectory = Environment.SpecialFolder.Desktop.ToString();
                if (fd.ShowDialog() == DialogResult.OK)
                {
                    ComboBox temp = ((ComboBox)ctlLayoutPanel.Controls.Find(((Button)sender).Tag.ToString().Replace(";", string.Empty), true)[0]);
                    if (File.Exists(fd.FileName))
                    {
                        temp.Items.Insert(0, fd.FileName);
                        temp.SelectedIndex = 0;
                    }
                    else
                    {
                        MessageBox.Show("Seçilien dosya bulunamadı","Uyarı");
                    }

                }
            }
        }

        private void ctlCancel_Click(object sender, EventArgs e)
        {
            object o = null;
            o.Equals(o);
            this.Close();
        }

        private void SetContentControls(AutoComplateInfoList list)
        {
            object index; string title;
            for (int i = 1; i <= Globals.AutoComplateAddIn.Application.ActiveDocument.ContentControls.Count; i++)
            {
                index = i;
                object x = Type.Missing;
                Microsoft.Office.Interop.Word.ContentControl control = Globals.AutoComplateAddIn.Application.ActiveDocument.ContentControls.get_Item(ref index);
                title = (string.IsNullOrEmpty(control.Title)) ? "NoTitle" + (i - 1).ToString() : control.Title;
                AutoCompleteInfo info = list.List.FirstOrDefault(p => p.FieldName == title);
                if (info != null)
                {
                    try
                    {
                        if (control.Type == WdContentControlType.wdContentControlPicture)
                        { control.Range.InlineShapes.AddPicture(info.Values[0], ref x, ref x, ref x); }
                        else control.Range.Text = info.Values[0];
                    }
                    catch { ;}
                }
            }
        }

        private string GetIndex(string tag)
        {
            int index = tag.IndexOf(";") + 1;
            return tag.Substring(index, (tag.Length - index));
        }
        private string GetTitle(string tag)
        {
            int index = tag.IndexOf(";");
            return tag.Substring(0, index);
        }

        private void ctlSave_Click(object sender, EventArgs e)
        {
            AutoComplateInfoList infolist = new AutoComplateInfoList();
            infolist.TemplateName = baselist.TemplateName;
            Control itemcontrol = null;
            int ctr = 0; string temptitle;
            foreach (Control control in ctlLayoutPanel.Controls)
            {
                itemcontrol = control;
                if (itemcontrol is TableLayoutPanel)
                {
                    foreach (Control childcontrol in itemcontrol.Controls)
                    {
                        if (childcontrol is ComboBox || childcontrol is TextBox)
                        {
                            itemcontrol = childcontrol;
                            break;
                        }
                    }
                }
                if (itemcontrol is ComboBox || itemcontrol is TextBox)
                {
                    AutoCompleteInfo info = new AutoCompleteInfo();
                    temptitle = GetTitle(itemcontrol.Tag.ToString());
                    info.FieldName = (temptitle == "NoTitle") ? "NoTitle" + ctr.ToString() : temptitle;
                    info.Values.Add(itemcontrol.Text.Trim());
                    infolist.List.Add(info);
                    ctr++;
                }
            }
            SetContentControls(infolist);
            infolist.List.RemoveAll(p => p.FieldName.IndexOf("NoTitle") != -1);
            AutoComplateManager.AddValue(infolist);
            this.Close();
        }

        private void ctlClear_Click(object sender, EventArgs e)
        {
            foreach (Control control in ctlLayoutPanel.Controls)
            {
                if (control is Panel)
                {
                    foreach (Control itemcontrol in control.Controls)
                    {
                        if (itemcontrol is ComboBox)
                        {
                            (itemcontrol as ComboBox).SelectedIndex = -1;
                        }
                    }
                }
            }
        }


        private void ctlLayoutPanel_SizeChanged(object sender, EventArgs e)
        {
            if (((TableLayoutPanel)sender).Height >= this.Height - 86)
            {
                ctlLayoutPanel.AutoScroll = true;
            }
            else ctlLayoutPanel.AutoScroll = false;
        }

        private void AutoCompleteManageForm_Load(object sender, EventArgs e)
        {

        }

        private void ctlAllFill_Click(object sender, EventArgs e)
        {

            foreach (Control control in ctlLayoutPanel.Controls)
            {
                if (control is Panel)
                {
                    foreach (Control itemcontrol in control.Controls)
                    {
                        if (itemcontrol is ComboBox)
                        {
                            ComboBox combocontrol = (ComboBox)itemcontrol;
                            foreach (AutoComplateInfoList itemlist in AutoComplateManager.List)
                            {
                                AutoCompleteInfo info = itemlist.List.SingleOrDefault(p => p.FieldName == GetTitle(combocontrol.Tag.ToString()));

                                if (info != null)
                                {
                                    foreach (string item in info.Values)
                                    {
                                        if (combocontrol.Items.IndexOf(item) == -1) combocontrol.Items.Add(item);
                                    }
                                }
                            }
                            if (combocontrol.Items.Count >= 1) combocontrol.SelectedIndex = 0;
                        }
                    }
                }
            }






        }









    }
}
